package com.example.demo.service;

import com.example.demo.mapper.SubscribeMapper;
import com.example.demo.tools.ResponseBodyMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class SubscribeService {
    // 注入 SubscribeMapper 依赖
    @Autowired
    private SubscribeMapper subscribeMapper;

    @Autowired
    private MailService mailService;

    public ResponseBodyMessage<Boolean> add(String email) {
        if (subscribeMapper.insertSubscribe( email )== 1) {
            mailService.sendMail("订阅成功", email);
            return new ResponseBodyMessage<>(200,"订阅成功", true);
        } else {
            return new ResponseBodyMessage<>(400, "订阅失败", false);
        }
    }

    public ResponseBodyMessage<Boolean> delete(String email) {
        if (subscribeMapper.deleteByEmail(email) > 0) {
            return new ResponseBodyMessage<>(200,"取消订阅成功", true);
        } else {
            return new ResponseBodyMessage<>(400, "取消订阅失败", false);
        }
    }
}
